<?php


namespace app\api\controller\v1;

use app\api\exception\BaseException;
use app\api\validate\ClassScene;
use app\api\validate\GoodsBrandScene;
use app\api\validate\GoodsClassScene;
use app\api\validate\GoodsDepotScene;
use app\api\validate\GoodsWeightScene;
use app\api\validate\PositionScene;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Request;

/**
 * Class GoodsClassScene
 * @package app\api\controller\v1
 *
 * 产品分类
 */
class GoodsClass extends BaseException
{
    /**
     * 不需要登录方法
     */
    protected $noAuth = ['login'];
    /************************************ 产品分类 ************************************
    /**
     *
     * 添加产品分类
     * 分类下有产品的品牌
     */
    public function addGoodsClass(){
        $request = Request::param();
        try {
            validate(GoodsClassScene::class)
                ->scene('add')
                ->check($request);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return self::Error(null, $e->getError(), 404);
        }
        $insertData = [
            "name" => $request["name"],
            'create_time' => date("Y-m-d H:i:s", time()),
            'update_time' => date("Y-m-d H:i:s", time()),
            'data_state' => 1
        ];
        $result = Db::name("goods_class")->insert($insertData);
        if ($result) {
            return self::Success(null, "添加成功", 200);
        } else {
            return self::Error(null, '添加失败', 10002);
        }
    }

    /**
     *
     * 获取产品分类列表
     */
    public function getGoodsClass(){
        $data = Request::param();
        if ($this->user_id > 2) {
            return self::Error(null, "当前账号无查看用户信息权限", 10002);
        }
        $fieldStr = "id,name,data_state,create_time,update_time";
        $search = [
        ];
        /**
         * 单一模糊查询开始
         */
        //名称模糊查询
        if (isset($data["name"]) && !empty($data["name"])) {
            $search[] = ["name", "like", "%" . $data["name"] . "%"];
        }

        /**
         * 单一模糊查询结束
         */
        /**
         *多条件查询开始
         */
        //数据状态筛选查询
        //  if (isset($data["data_state"]) && $data["data_state"] != '') {
        array_push($search, ["data_state", "=", 1]);
        //}
        $goodsClasslist = Db::name("goods_class")->where($search)->field($fieldStr)->order("id asc")->page($this->page_num, $this->page_limit)->select()->toArray();
        $count = Db::name("goods_class")->where($search)->count();
        foreach ($goodsClasslist as $key => $value){
            $brandList = Db::name("brand")->where(["class_id" => $goodsClasslist[$key]["id"]])->select()->toArray();
            $goodsClasslist[$key]["children"] = $brandList;
        }
        $list = [
            "total" => $count,
            "page" => $this->page_num,
            "limit" => $this->page_limit,
            "list" => $goodsClasslist
        ];
        return self::Success($list, '获取成功', 200);
    }
    /**
     *
     * 公共查询商品分类
     */
    public function publicGoodsClass(){
        $goodsClassList = Db::name("goods_class")->where(["data_state" => 1])->column("id,name");
        foreach ($goodsClassList as $key => $value){
            $check = Db::name("brand")->where(["class_id" => $value["id"],"data_state" => 1])->find();
            if (empty($check)){
                unset($goodsClassList[$key]);
            }

        }
        return self::Success($goodsClassList, "获取成功", 200);
    }

    /**
     *
     * 修改产品分类信息
     */
    public function updateGoodsClassInfo(){
        $request = Request::param();
        try {
            validate(GoodsClassScene::class)
                ->scene('update')
                ->check($request);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return self::Error(null, $e->getError(), 404);
        }
        $check = Db::name("goods_class")->where(["id" => $request["id"]])->find();
        if ($check) {
            $updateData = [
                "name" => $request["name"],
                'update_time' => date("Y-m-d H:i:s", time()),
            ];
            $result = Db::name("goods_class")->where(["id" => $request["id"]])->update($updateData);
            if ($result) {
                return self::Success(null, "更新成功", 200);
            } else {
                return self::Error(null, '更新失败或已更新', 10002);
            }
        } else {
            return self::Error(null, '分类不存在', 10002);
        }
    }

    /**
     *
     * 删除分类
     * 不建议删除分类
     * 只有分类下无品牌的时候才能删除
     */
    public function UpdateGoodsClassState(){
        $request = Request::param();
        try {
            validate(GoodsClassScene::class)
                ->scene('update_state')
                ->check($request);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return self::Error(null, $e->getError(), 404);
        }
        $updateData = [
            'update_time' => date("Y-m-d H:i:s", time()),
            'data_state' => $request["data_state"]
        ];
        $check = Db::name("goods_class")->where(["id" => $request["id"]])->find();
        if ($check) {
            $result = Db::name("goods_class")->where(["id" => $request["id"]])->update($updateData);
            if ($result) {
                return self::Success(null, "操作成功", 200);
            } else {
                return self::Error(null, '操作失败', 10002);
            }
        } else {
            return self::Error(null, '分类不存在', 10002);
        }
    }
    /************************************ 产品品牌 ************************************
    /**
     *
     * 添加产品品牌 根据分类添加品牌
     */
    public function addGoodsBrand(){
        $request = Request::param();
        try {
            validate(GoodsBrandScene::class)
                ->scene('add')
                ->check($request);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return self::Error(null, $e->getError(), 404);
        }
        $check_one = Db::name("brand")->where(["class_id" => $request["class_id"],"name" => $request["name"]])->find();
        if ($check_one){
            return self::Error(null, '同一分类下，名称不能相同', 10002);
        }
        $check = Db::name("goods_class")->where(["id" => $request["class_id"],"data_state" => 1])->find();
        if (!$check){
            return self::Error(null, '分类不存在', 10002);
        }
        $insertData = [
            "name" => $request["name"],
            "class_id" => $request["class_id"],
            'create_time' => date("Y-m-d H:i:s", time()),
            'update_time' => date("Y-m-d H:i:s", time()),
            'data_state' => 1
        ];
        $result = Db::name("brand")->insert($insertData);
        if ($result) {
            return self::Success(null, "添加成功", 200);
        } else {
            return self::Error(null, '添加失败', 10002);
        }
    }

    /**
     *
     * 更新产品品牌
     */
    public function updateGoodsBrand(){
        $request = Request::param();
        try {
            validate(GoodsBrandScene::class)
                ->scene('update')
                ->check($request);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return self::Error(null, $e->getError(), 404);
        }
        $check_one = Db::name("brand")->where(["class_id" => $request["class_id"],"name" => $request["name"]])->find();
        if ($check_one){
            return self::Error(null, '同一分类下，名称不能相同', 10002);
        }
        $check = Db::name("goods_class")->where(["id" => $request["class_id"],"data_state" => 1])->find();
        if (!$check){
            return self::Error(null, '分类不存在', 10002);
        }
        $check = Db::name("brand")->where(["id" => $request["id"]])->find();
        if ($check) {
            $updateData = [
                "class_id" => $request["class_id"],
                "name" => $request["name"],
                'update_time' => date("Y-m-d H:i:s", time()),
            ];
            $result = Db::name("brand")->where(["id" => $request["id"]])->update($updateData);
            if ($result) {
                return self::Success(null, "更新成功", 200);
            } else {
                return self::Error(null, '更新失败或已更新', 10002);
            }
        } else {
            return self::Error(null, '品牌不存在', 10002);
        }
    }

    /**
     *
     * 删除
     * 更新产品品牌数据状态
     */
    public function updateGoodsBrandState(){
        $request = Request::param();
        try {
            validate(GoodsBrandScene::class)
                ->scene('update_state')
                ->check($request);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return self::Error(null, $e->getError(), 404);
        }
        $updateData = [
            'update_time' => date("Y-m-d H:i:s", time()),
            'data_state' => $request["data_state"]
        ];
        $check = Db::name("brand")->where(["id" => $request["id"]])->find();
        if ($check) {
            $result = Db::name("brand")->where(["id" => $request["id"]])->update($updateData);
            if ($result) {
                return self::Success(null, "操作成功", 200);
            } else {
                return self::Error(null, '操作失败', 10002);
            }
        } else {
            return self::Error(null, '品牌不存在', 10002);
        }
    }

    /**
     *
     * 根据产品分类获取产品品牌
     */
    public function getGoodsBrandList(){
        $data = Request::param();
        if ($this->user_id > 2) {
            return self::Error(null, "当前账号无查看用户信息权限", 10002);
        }
        $fieldStr = "id,class_id,name,data_state,create_time,update_time";
        $search = [
        ];
        /**
         * 单一模糊查询开始
         */
        //名称模糊查询
        if (isset($data["name"]) && !empty($data["name"])) {
            $search[] = ["name", "like", "%" . $data["name"] . "%"];
        }

        /**
         * 单一模糊查询结束
         */
        /**
         *多条件查询开始
         */
        //数据状态筛选查询
        if (isset($data["data_state"]) && $data["data_state"] != '') {
            array_push($search, ["data_state", "=", $data["data_state"]]);
        }

        if (isset($data["class_id"]) && $data["class_id"] != '') {
            array_push($search, ["class_id", "=", $data["class_id"]]);
        }
        $brandlist = Db::name("brand")->where($search)->field($fieldStr)->order("id asc")->page($this->page_num, $this->page_limit)->select()->toArray();
        $count = Db::name("brand")->where($search)->count();
        foreach ($brandlist as $key => $value){
            $goods_class_name = Db::name("goods_class")->where(["id" => $brandlist[$key]["class_id"],"data_state" => 1])->value("name");
            $brandlist[$key]["class_name"] = empty($goods_class_name) ? "暂无分类":$goods_class_name;
        }
        $list = [
            "total" => $count,
            "page" => $this->page_num,
            "limit" => $this->page_limit,
            "list" => $brandlist
        ];
        //Db::name("account")->getLastSql();
        return self::Success($list, '获取成功', 200);
    }

    /**
     *
     * 公共查询品牌 根据产品分类查询
     */
    public function publicGoodsBrand(){
        $data = Request::param();
        $goodsBrandList = Db::name("brand")->where(["data_state" => 1,"class_id" => $data["goods_class"]])->column("id,name");
        return self::Success($goodsBrandList, "获取成功", 200);
    }

    /************************************ 产品仓库 ************************************
    /**
     *
     * 添加产品仓库
     */
    public function addGoodsDepot(){
        $request = Request::param();
        try {
            validate(GoodsDepotScene::class)
                ->scene('add')
                ->check($request);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return self::Error(null, $e->getError(), 404);
        }
        $insertData = [
            "name" => $request["name"],
            'create_time' => date("Y-m-d H:i:s", time()),
            'update_time' => date("Y-m-d H:i:s", time()),
            'data_state' => 1
        ];
        $result = Db::name("goods_depot")->insert($insertData);
        if ($result) {
            return self::Success(null, "添加成功", 200);
        } else {
            return self::Error(null, '添加失败', 10002);
        }
    }

    /**
     *
     * 更新产品仓库信息
     */
    public function updateGoodsDepot(){
        $request = Request::param();
        try {
            validate(GoodsDepotScene::class)
                ->scene('update')
                ->check($request);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return self::Error(null, $e->getError(), 404);
        }
        $check = Db::name("goods_depot")->where(["id" => $request["id"]])->find();
        if ($check) {
            $updateData = [
                "name" => $request["name"],
                'update_time' => date("Y-m-d H:i:s", time()),
            ];
            $result = Db::name("goods_depot")->where(["id" => $request["id"]])->update($updateData);
            if ($result) {
                return self::Success(null, "更新成功", 200);
            } else {
                return self::Error(null, '更新失败或已更新', 10002);
            }
        } else {
            return self::Error(null, '仓库不存在', 10002);
        }
    }

    /**
     *
     * 更改数据状态
     * 假删除
     */
    public function updateGoodsDepotState(){
        $request = Request::param();
        try {
            validate(GoodsDepotScene::class)
                ->scene('update_state')
                ->check($request);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return self::Error(null, $e->getError(), 404);
        }
        $updateData = [
            'update_time' => date("Y-m-d H:i:s", time()),
            'data_state' => $request["data_state"]
        ];
        $check = Db::name("goods_depot")->where(["id" => $request["id"]])->find();
        if ($check) {
            $result = Db::name("goods_depot")->where(["id" => $request["id"]])->update($updateData);
            if ($result) {
                return self::Success(null, "操作成功", 200);
            } else {
                return self::Error(null, '操作失败', 10002);
            }
        } else {
            return self::Error(null, '仓库不存在', 10002);
        }
    }

    /**
     *
     * 获取仓库列表
     */
    public function getGoodsDepotList(){
        $data = Request::param();
        if ($this->user_id > 2) {
            return self::Error(null, "当前账号无查看用户信息权限", 10002);
        }
        $fieldStr = "id,name,data_state,create_time,update_time";
        $search = [
        ];
        /**
         * 单一模糊查询开始
         */
        //名称模糊查询
        if (isset($data["name"]) && !empty($data["name"])) {
            $search[] = ["name", "like", "%" . $data["name"] . "%"];
        }

        /**
         * 单一模糊查询结束
         */
        /**
         *多条件查询开始
         */
        //数据状态筛选查询
        if (isset($data["data_state"]) && $data["data_state"] != '') {
            array_push($search, ["data_state", "=", $data["data_state"]]);
        }
        $goodsDepotList = Db::name("goods_depot")->where($search)->field($fieldStr)->order("id asc")->page($this->page_num, $this->page_limit)->select();
        $count = Db::name("goods_depot")->where($search)->count();
        $list = [
            "total" => $count,
            "page" => $this->page_num,
            "limit" => $this->page_limit,
            "list" => $goodsDepotList
        ];
        //Db::name("account")->getLastSql();
        return self::Success($list, '获取成功', 200);
    }
    /**
     *
     * 公共仓库列表
     */
    public function publicGoodsDepot(){
        $goodsDepotList = Db::name("goods_depot")->where(["data_state" => 1])->column("id,name");
        return self::Success($goodsDepotList, "获取成功", 200);
    }


    /************************************ 产品重量 ************************************

    /**
     *
     * 添加产品重量信息
     */
    public function addGoodsWeight(){
        $request = Request::param();
        try {
            validate(GoodsWeightScene::class)
                ->scene('add')
                ->check($request);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return self::Error(null, $e->getError(), 404);
        }
        $insertData = [
            "num" => $request["num"],
            'create_time' => date("Y-m-d H:i:s", time()),
            'update_time' => date("Y-m-d H:i:s", time()),
            'data_state' => 1
        ];
        $result = Db::name("goods_weight")->insert($insertData);
        if ($result) {
            return self::Success(null, "添加成功", 200);
        } else {
            return self::Error(null, '添加失败', 10002);
        }
    }

    /**
     *
     * 更新产品重量信息
     */
    public function updateGoodsWeight(){
        $request = Request::param();
        try {
            validate(GoodsWeightScene::class)
                ->scene('update')
                ->check($request);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return self::Error(null, $e->getError(), 404);
        }
        $check = Db::name("goods_weight")->where(["id" => $request["id"]])->find();
        if ($check) {
            $updateData = [
                "num" => $request["num"],
                'update_time' => date("Y-m-d H:i:s", time()),
            ];
            $result = Db::name("goods_weight")->where(["id" => $request["id"]])->update($updateData);
            if ($result) {
                return self::Success(null, "更新成功", 200);
            } else {
                return self::Error(null, '更新失败或已更新', 10002);
            }
        } else {
            return self::Error(null, '重量不存在', 10002);
        }
    }

    /**
     *
     * 更改数据状态
     * 假删除
     */
    public function updateGoodsWeightState(){
        $request = Request::param();
        try {
            validate(GoodsWeightScene::class)
                ->scene('update_state')
                ->check($request);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return self::Error(null, $e->getError(), 404);
        }
        $updateData = [
            'update_time' => date("Y-m-d H:i:s", time()),
            'data_state' => $request["data_state"]
        ];
        $check = Db::name("goods_weight")->where(["id" => $request["id"]])->find();
        if ($check) {
            $result = Db::name("goods_weight")->where(["id" => $request["id"]])->update($updateData);
            if ($result) {
                return self::Success(null, "操作成功", 200);
            } else {
                return self::Error(null, '操作失败', 10002);
            }
        } else {
            return self::Error(null, '分类不存在', 10002);
        }
    }

    /**
     *
     * 获取重量列表
     */
    public function getGoodsWeightList(){
        $data = Request::param();
        if ($this->user_id > 2) {
            return self::Error(null, "当前账号无查看用户信息权限", 10002);
        }
        $fieldStr = "id,num,data_state,create_time,update_time";
        $search = [
        ];

        /**
         * 单一模糊查询结束
         */
        /**
         *多条件查询开始
         */
        //数据状态筛选查询
        if (isset($data["data_state"]) && $data["data_state"] != '') {
            array_push($search, ["data_state", "=", $data["data_state"]]);
        }
        $goodsWeightList = Db::name("goods_weight")->where($search)->field($fieldStr)->order("id asc")->page($this->page_num, $this->page_limit)->select();
        $count = Db::name("goods_weight")->where($search)->count();
        $list = [
            "total" => $count,
            "page" => $this->page_num,
            "limit" => $this->page_limit,
            "list" => $goodsWeightList
        ];
        //Db::name("account")->getLastSql();
        return self::Success($list, '获取成功', 200);
    }
    /**
     *
     * 公共重量列表
     */
    public function publicGoodsWeight(){
        $goodsWeightList = Db::name("goods_weight")->where(["data_state" => 1])->column("id,num");
        return self::Success($goodsWeightList, "获取成功", 200);
    }





}